package com.ruoyi.web.controller.module.estate;

import java.util.List;
import java.util.Arrays;

import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.system.domain.module.estate.EstateContract;
import com.ruoyi.system.service.module.estate.IEstateContractService;
import lombok.RequiredArgsConstructor;
import javax.validation.constraints.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 合同列Controller
 *
 * @author ruoyi
 * @date 2023-12-26
 */
@Validated
@Api(value = "合同列控制器", tags = {"合同列管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/module/estate/contract")
public class EstateContractController extends BaseController {

    private final IEstateContractService estateContractService;

    /**
     * 查询合同列列表
     */
    @ApiOperation("查询合同列列表")
    @PreAuthorize("@ss.hasPermi('module:estateContract:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateContract> list(@Validated EstateContract estateContract) {
        QueryWrapper<EstateContract> qw = new QueryWrapper<>();

        TableDataInfo<EstateContract> td = PageUtils.buildDataInfo(estateContractService.page(PageUtils.buildPage(),qw));
        List<EstateContract> list = td.getRows();
        return td;
    }

    /**
     * 导出合同列列表
     */
    @ApiOperation("导出合同列列表")
    @PreAuthorize("@ss.hasPermi('module:estateContract:export')")
    @Log(title = "合同列", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult<EstateContract> export(@Validated EstateContract estateContract) {
        List<EstateContract> list = estateContractService.list();
        ExcelUtil<EstateContract> util = new ExcelUtil<EstateContract>(EstateContract.class);
        return util.exportExcel(list, "合同列");
    }

    /**
     * 获取合同列详细信息
     */
    @ApiOperation("获取合同列详细信息")
    @PreAuthorize("@ss.hasPermi('module:estateContract:query')")
    @GetMapping("/{id}")
    public AjaxResult<EstateContract> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateContract estateContract = estateContractService.getById(id);
        return AjaxResult.success(estateContract);
    }

    /**
     * 新增合同列
     */
    @ApiOperation("新增合同列")
    @PreAuthorize("@ss.hasPermi('module:estateContract:add')")
    @Log(title = "合同列", businessType = BusinessType.INSERT)
    @RepeatSubmit
    @PostMapping()
    public AjaxResult<Void> add(@Validated(AddGroup.class) @RequestBody EstateContract estateContract) {
        return toAjax(estateContractService.save(estateContract) ? 1 : 0);
    }

    /**
     * 修改合同列
     */
    @ApiOperation("修改合同列")
    @PreAuthorize("@ss.hasPermi('module:estateContract:edit')")
    @Log(title = "合同列", businessType = BusinessType.UPDATE)
    @RepeatSubmit
    @PutMapping()
    public AjaxResult<Void> edit(@Validated(EditGroup.class) @RequestBody EstateContract estateContract) {
        return toAjax(estateContractService.updateById(estateContract) ? 1 : 0);
    }

    /**
     * 删除合同列
     */
    @ApiOperation("删除合同列")
    @PreAuthorize("@ss.hasPermi('module:estateContract:remove')")
    @Log(title = "合同列" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return toAjax(estateContractService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
}
